package com.hospital.service.publics.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.graphbuilder.curve.CubicBSpline;
import com.hospital.service.publics.model.param.*;
import com.hospital.service.publics.model.vo.PubUserInfoVo2;
import common.core.util.CustomizeRuntimeException;
import common.core.util.SystemLog;
import enhance.plus.logic.LogicBaseController;
import com.hospital.service.publics.entity.PubUserInfo;
import com.hospital.service.publics.entity.help.PubUserInfoHelp;
import com.hospital.service.publics.service.PubUserInfoService;
import com.hospital.service.publics.model.vo.PubUserInfoVo;
import common.core.util.CommonUtil;
import common.core.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import enhance.plus.logic.LogicBaseController;
import enhance.plus.util.Page;
import enhance.plus.util.PageHelper;


import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Map;


/**
 * 用户信息表 控制器
 *
 * @author xhl
 * @created 2020-10-13 14:51:12
 */
@Slf4j
@Validated
@RestController
@Api(tags = "用户信息表")
@RequestMapping("/publics/pubUserInfo")
public class PubUserInfoController extends LogicBaseController<PubUserInfoService, PubUserInfo> {

    @Autowired
    private PubUserInfoService pubUserInfoService;

    @ApiOperation(value = "保存用户信息表的数据")
    @SystemLog(value = "添加用户", logo = "userNumber")
    @RequestMapping(value = "/save" , method = RequestMethod.PUT)
    public Result save(@Valid @RequestBody PubUserInfoParam param){
        log.debug("判断当前用户的编号是否存在{}", param.getUserNumber());
        QueryWrapper<PubUserInfo> query = PubUserInfoHelp.createQuery();
        query.eq(PubUserInfoHelp.userNumber, param.getUserNumber());
        int count = this.pubUserInfoService.count(query);
        if (count > 0) {
            throw CustomizeRuntimeException.exception("此编号已存在");
        }
        log.debug("开始保存用户信息表-->{}",param);
        return Result.success(this.pubUserInfoService.save(CommonUtil.copyBean(param,PubUserInfo.class)));
    }

    @ApiOperation(value = "更新用户信息表的数据")
    @SystemLog(value = "更新用户信息", logo = "userNumber")
    @RequestMapping(value = "/update" , method = RequestMethod.PUT)
    public Result update(@Valid @RequestBody PubUserInfoUpdateParam param){
        log.debug("开始跟新用户信息表-->{}",param);
        return Result.success(this.pubUserInfoService.updateById(CommonUtil.copyBean(param,PubUserInfo.class)));
    }

    @ApiOperation(value = "删除用户信息表的一条数据")
    @RequestMapping(value = "/delete" , method = RequestMethod.DELETE)
    @SystemLog(value = "删除用户信息", logo = "id")
    public Result delete(@Valid PubUserInfoDeleteParam param){
        return Result.success(this.pubUserInfoService.removeById(param.getId()));
    }

    @ApiOperation(value = "根据条件查询一条用户信息表的数据")
    @RequestMapping(value = "/queryForOneRow" , method = RequestMethod.POST)
    public Result<PubUserInfoVo> queryForOneRow(@Valid @RequestBody PubUserInfoQueryForOneRowParam param){
        PubUserInfo row = this.pubUserInfoService.getOne(param.convertParamToQueryWapper());
        return Result.success( CommonUtil.copyBean(row, PubUserInfoVo.class));
    }

    @ApiOperation(value = "根据条件分页查询用户信息表的数据")
    @RequestMapping(value = "/queryPage" , method = RequestMethod.POST)
    public Result<Page<PubUserInfoVo2>> queryPage(@Valid @RequestBody PubUserInfoQueryPageParam param){
        return Result.success(this.pubUserInfoService.page(param.page(),param.convertParamToQueryWapper()
                .orderByDesc(PubUserInfoHelp.createTime)));
    }

    @ApiOperation(value = "得到所有的用户信息")
    @RequestMapping(value = "/getAllUser" , method = RequestMethod.POST)
    public Result<PubUserInfoVo> getAllUser(){
        List<PubUserInfo> list = this.pubUserInfoService.list();
        List<Object> allUser = CommonUtil.copyListBean(list, PubUserInfoVo.class);
        return Result.success(allUser);
    }

    @ApiOperation(value = "得到用户编号")
    @RequestMapping(value = "/getNumber" , method = RequestMethod.POST)
    public Result<String> getNumber(){
        return Result.success(CommonUtil.getCurrentDateTimeByFormat("yyyyMMddHHmmss"));
    }
}
